<?

/*

TODO

* default config for mailer

*/

require_once(dirname(__FILE__) . '/../libs/bf/bf.php');

$argv = bf_array::from_array(@$_SERVER['argv']);

if ($filename = $argv->get(1)) {

	if ($filename == '--help') {
		usage();
		exit(0);
	}

	require($filename);

	$app = (object)$app;

	$path_prefix = dirname(__FILE__) . "/../applications/{$app->name}";
	$path = array(
		'app' => "{$path_prefix}",
		'js' => "{$path_prefix}/js",
		'css' => "{$path_prefix}/css",
		'images' => "{$path_prefix}/images",
		'views' => "{$path_prefix}/views",
		'models' => "{$path_prefix}/models",
		'config' => "{$path_prefix}/config",
		'controllers' => "{$path_prefix}/controllers",
		'validators' => "{$path_prefix}/validators",
		'src' => "{$path_prefix}/src",
		'data' => "{$path_prefix}/data",
		'tmp' => "{$path_prefix}/tmp",
		'logs' => "{$path_prefix}/logs",
	);

	if (!@$app->location)
		$app->location = $app->name;

	if (!@$app->copyright)
		$app->copyright = $app->name;

	if (!@$app->menu)
		$app->menu = array();

	bf::$application = new stdClass();
	bf::$application->name = 'bf-tools';
	bf::$application->views = dirname(__FILE__) . '/application.templates';

	create_file("{$path['app']}/bootstrap.php", bf::view('bootstrap', array('app' => $app), true));

	foreach ($path as $p)
		@mkdir($p, 0777, true);

	foreach (scandir(bf::$application->views . '/config') as $tmpl)
		if (!preg_match('/^\./', $tmpl) /*&& is_file($tmpl)*/)
			create_file("{$path['config']}/$tmpl", bf::view("config/$tmpl", array('app' => $app), true));

	foreach (scandir(bf::$application->views . '/css') as $tmpl)
		if (!preg_match('/^\./', $tmpl) /*&& is_file($tmpl)*/)
			create_file("{$path['css']}/$tmpl", bf::view("css/$tmpl", array('app' => $app), true));

	foreach (scandir(bf::$application->views . '/js') as $tmpl)
		if (!preg_match('/^\./', $tmpl) /*&& is_file($tmpl)*/)
			create_file("{$path['js']}/$tmpl", bf::view("js/$tmpl", array('app' => $app), true));

	foreach (scandir(bf::$application->views . '/controllers') as $tmpl)
		if (!preg_match('/^\./', $tmpl) /*&& is_file($tmpl)*/)
			create_file("{$path['controllers']}/$tmpl", bf::view("controllers/$tmpl", array('app' => $app), true));

	foreach (scandir(bf::$application->views . '/views/chunks') as $tmpl)
		if (!preg_match('/^\./', $tmpl) /*&& is_file($tmpl)*/)
			create_file("{$path['views']}/chunks/$tmpl", bf::view("views/chunks/$tmpl", array('app' => $app), true));

	foreach (scandir(bf::$application->views . '/views') as $tmpl)
		if (is_file(bf::$application->views . '/views/' . $tmpl))
			create_file("{$path['views']}/$tmpl", bf::view("views/$tmpl", array('app' => $app), true));

	@copy($filename, $path['src'] . '/' . basename($filename));

} else {

	usage();
}

function create_file($name, $content) {
	echo "$name\n";
	@mkdir(dirname($name), 0777, true);
	return file_put_contents($name, $content);
}

function usage() {
	$name = basename(__FILE__);
	echo "usage:\n";
	echo "\tphp $name --help\n";
	echo "\tphp $name <file.app>\n";
}

?>